﻿using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MKAdminCore.ToolKit.RedisHelper
{
    /// <summary>
    /// 发布订阅
    /// </summary>
    public class RedisSubscribeHelper
    {
        #region 构造函数
        /// <summary>
        /// 初始化Redis的String数据结构操作
        /// </summary>
        /// <param name="dbNum">操作的数据库索引0-64(需要在conf文件中配置)</param>
        static RedisSubscribeHelper()
        {
            RedisManager.CreateConn();
        }
        #endregion

        #region 发布订阅

        #region 同步

        /// <summary>
        /// 订阅
        /// </summary>
        /// <param name="subChannel"></param>
        /// <param name="handler"></param>
        public static void Subscribe(string subChannel, Action<RedisChannel, RedisValue> handler = null)
        {
            ISubscriber sub = RedisManager._instance.GetSubscriber();
            sub.Subscribe(subChannel, (channel, message) =>
            {
                if (handler == null)
                {
                    Console.WriteLine(subChannel + " 订阅收到消息：" + message);
                }
                else
                {
                    handler(channel, message);
                }
            });
        }

        /// <summary>
        /// 发布
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="channel"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static long Publish<T>(string channel, T msg)
        {
            ISubscriber sub = RedisManager._instance.GetSubscriber();
            return sub.Publish(channel, RedisManager.ConvertJson(msg));
        }

        /// <summary>
        /// 取消订阅
        /// </summary>
        /// <param name="channel"></param>
        public static void Unsubscribe(string channel)
        {
            ISubscriber sub = RedisManager._instance.GetSubscriber();
            sub.Unsubscribe(channel);
        }

        /// <summary>
        /// 取消全部订阅
        /// </summary>
        public static void UnsubscribeAll()
        {
            ISubscriber sub = RedisManager._instance.GetSubscriber();
            sub.UnsubscribeAll();
        }

        #endregion 发布订阅

        #region 异步

        /// <summary>
        /// 订阅
        /// </summary>
        /// <param name="subChannel"></param>
        /// <param name="handler"></param>
        public async static Task SubscribeAsync(string subChannel, Action<RedisChannel, RedisValue> handler = null)
        {
            ISubscriber sub = RedisManager._instance.GetSubscriber();
            await sub.SubscribeAsync(subChannel, (channel, message) =>
            {
                if (handler == null)
                {
                    Console.WriteLine(subChannel + " 订阅收到消息：" + message);
                }
                else
                {
                    handler(channel, message);
                }
            });
        }

        /// <summary>
        /// 发布
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="channel"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public async static Task<long> PublishAsync<T>(string channel, T msg)
        {
            ISubscriber sub = RedisManager._instance.GetSubscriber();
            return await sub.PublishAsync(channel, RedisManager.ConvertJson(msg));
        }

        /// <summary>
        /// 取消订阅
        /// </summary>
        /// <param name="channel"></param>
        public async static Task UnsubscribeAsync(string channel)
        {
            ISubscriber sub = RedisManager._instance.GetSubscriber();
            await sub.UnsubscribeAsync(channel);
        }

        /// <summary>
        /// 取消全部订阅
        /// </summary>
        public async static Task UnsubscribeAllAsync()
        {
            ISubscriber sub = RedisManager._instance.GetSubscriber();
            await sub.UnsubscribeAllAsync();
        }

        #endregion 发布订阅

        #endregion
    }
}
